package com.lovo;

import java.sql.*;

public class Test06 {
    public static Connection getConnection(){
        Connection con = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            con = DriverManager.getConnection(
                    "jdbc:mysql://localhost/j189?userSSL=true&useUnicode=true&characterEncoding=UTF8&serverTimezone=GMT",
                    "root","lovo");
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
        return con;
    }
    public void list(){
        Connection con = getConnection();
        System.out.println(con);
        Statement statement = null;
        ResultSet rs = null;
        String name = "张飞' or '1'='1";
        String sql = "select * from t_student where s_name='"+name+"'";
        System.out.println(sql);
        try {
            statement = con.createStatement();
            rs = statement.executeQuery(sql);
            while(rs.next()){
                System.out.println(rs.getString("s_name") + " " + rs.getInt("s_age"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            if(rs != null){
                try {
                    rs.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
            if(statement != null){
                try {
                    statement.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if(con != null){
                try {
                    con.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
    }
    public void insert(){
        Connection con = getConnection();
        System.out.println(con);
        Statement statement = null;
        String sql = "insert into t_student(s_name,s_age,s_gender) values('小刚',20,'男')";
        try {
            statement = con.createStatement();
            int r = statement.executeUpdate(sql);
            System.out.println("row:" + r);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            if(statement != null){
                try {
                    statement.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if(con != null){
                try {
                    con.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }


    }
    public static void main(String[] args) {
        Test06 test06 = new Test06();
//        test06.insert();
        test06.list();
    }
}
